package com.ruoyi.dispatch.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dispatch.domain.MonthSalesReport;
import com.ruoyi.dispatch.domain.SalesDayReport;
import com.ruoyi.dispatch.enums.SalesDayReportCol;
import com.ruoyi.dispatch.mapper.SalesDayReportMapper;
import com.ruoyi.dispatch.service.ISalesDayReportService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 *  销售日报 Service业务层处理
 * 
 * @author wuxiaofang
 * @date 2025-04-11
 */
@Service
public class SalesDayReportServiceImpl implements ISalesDayReportService 
{
    @Autowired
    private SalesDayReportMapper salesDayReportMapper;

    /**
     * 查询 销售日报 
     * 
     * @param reportId  销售日报 主键
     * @return  销售日报 
     */
    @Override
    public SalesDayReport selectSalesDayReportByReportId(Long reportId)
    {
        return salesDayReportMapper.selectSalesDayReportByReportId(reportId);
    }

    /**
     * 查询 销售日报 列表
     * 
     * @param salesDayReport  销售日报 
     * @return  销售日报 
     */
    @Override
    public List<SalesDayReport> selectSalesDayReportList(SalesDayReport salesDayReport)
    {
        return salesDayReportMapper.selectSalesDayReportList(salesDayReport);
    }

    /**
     * 新增 销售日报 
     * 
     * @param salesDayReport  销售日报 
     * @return 结果
     */
    @Override
    public int insertSalesDayReport(SalesDayReport salesDayReport)
    {
        salesDayReport.setCreateTime(DateUtils.getNowDate());
        return salesDayReportMapper.insertSalesDayReport(salesDayReport);
    }

    /**
     * 修改 销售日报 
     * 
     * @param salesDayReport  销售日报 
     * @return 结果
     */
    @Override
    public int updateSalesDayReport(SalesDayReport salesDayReport)
    {
        salesDayReport.setUpdateTime(DateUtils.getNowDate());
        return salesDayReportMapper.updateSalesDayReport(salesDayReport);
    }

    /**
     * 批量删除 销售日报 
     * 
     * @param reportIds 需要删除的 销售日报 主键
     * @return 结果
     */
    @Override
    public int deleteSalesDayReportByReportIds(Long[] reportIds)
    {
        return salesDayReportMapper.deleteSalesDayReportByReportIds(reportIds);
    }

    /**
     * 删除 销售日报 信息
     * 
     * @param reportId  销售日报 主键
     * @return 结果
     */
    @Override
    public int deleteSalesDayReportByReportId(Long reportId)
    {
        return salesDayReportMapper.deleteSalesDayReportByReportId(reportId);
    }

    @Override
    public List<MonthSalesReport> countSalesReport(SalesDayReport salesDayReport) {
        SimpleDateFormat sourceFormatMonth = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sourceFormatYear = new SimpleDateFormat("yyyy");
        String month = sourceFormatMonth.format(salesDayReport.getSaleDate());
        String year = sourceFormatYear.format(salesDayReport.getSaleDate());
        List<SalesDayReport> dayReportList = salesDayReportMapper.selectSalesDayReportList(salesDayReport);
        List<MonthSalesReport> monthList = salesDayReportMapper.monthSales(month);
        List<MonthSalesReport> yearSalesList = salesDayReportMapper.yearSales(year);
        for (SalesDayReport dayReport : dayReportList) {
            for (MonthSalesReport monthSalesReport : monthList) {
                if (dayReport.getCoalTypeId().equals(monthSalesReport.getCoalTypeId())){
                    BeanUtils.copyProperties( dayReport,monthSalesReport);
                }
                for (MonthSalesReport yearReport : yearSalesList) {
                    if (monthSalesReport.getCoalTypeId().equals(yearReport.getCoalTypeId())){
                        monthSalesReport.setYearSales(yearReport.getYearSales());
                    }
                }
            }
        }
        return monthList;
    }

    @Override
    public List<Map<String, String>> handleColumnMap(String[] columnArray) {
        List<Map<String, String>> columns = Lists.newArrayList();
        for (String item : columnArray) {
            Map column = SalesDayReportCol.getByCode(item);
            if (column != null) {
                columns.add(column);
            }
        }
        return columns;
    }
}
